package dev.alm.alertagentservice.service.impl;

import dev.alm.alertagentservice.AlertAgentServiceApplication;
import dev.alm.alertagentservice.bean.*;
import dev.alm.alertagentservice.service.ModelService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class ModelServiceImpl implements ModelService {

    @Resource
    private RestTemplate restTemplate;

    @Value("${service.url}")
    private String serviceUrl;


    @Override
    public List<DictTypeParam> getDictTypeList() {
        String url = serviceUrl + "/dmsap/dicttype/queryDictTypeParamList";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", AlertAgentServiceApplication.TOKEN);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> requestEntity = new HttpEntity<>(headers);

        ReqBody reqBody = new ReqBody();
        reqBody.setOffset(0);
        reqBody.setPageSize(100);
        // 创建包含请求体和请求头的HttpEntity
        HttpEntity<ReqBody> request = new HttpEntity<>(reqBody, headers);

        // 发送POST请求
        GetDictTypeParamRes getDictTypeParamRes = restTemplate.postForObject(url, request, GetDictTypeParamRes.class);
        List<DictTypeParam> data = getDictTypeParamRes.getResult().getData();

        return data;
    }

    @Override
    public String addDictType(DictTypeParam dictTypeParam) {
        String url = serviceUrl + "/dmsap/dicttype/saveDictType";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", AlertAgentServiceApplication.TOKEN);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<DictTypeParam> request = new HttpEntity<>(dictTypeParam, headers);
        CommonResult commonResult = restTemplate.postForObject(url, request, CommonResult.class);
        if (commonResult.getCode() != 200) {
            return commonResult.getMessage();
        }
        return "创建成功";
    }

    @Override
    public String removeDictType(DictTypeParam dictTypeParam) {
        String url = serviceUrl + "/dmsap/dicttype/deleteBatch";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", AlertAgentServiceApplication.TOKEN);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<DictTypeParam> request = new HttpEntity<>(dictTypeParam, headers);
        CommonResult commonResult = restTemplate.postForObject(url, request, CommonResult.class);
        if (commonResult.getCode() != 200) {
            return commonResult.getMessage();
        }
        return "删除成功";
    }
    @Override
    public String startDictType(UpdateDictTypeStatusParam updateDictTypeStatusParam) {
        String url = serviceUrl + "/dmsap/dicttype/updateDictTypeStatus";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", AlertAgentServiceApplication.TOKEN);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<UpdateDictTypeStatusParam> request = new HttpEntity<>(updateDictTypeStatusParam, headers);
        CommonResult commonResult = restTemplate.postForObject(url, request, CommonResult.class);
        if (commonResult.getCode() != 200) {
            return commonResult.getMessage();
        }
        return "启用成功";
    }


    @Override
    public String stopDictType(UpdateDictTypeStatusParam updateDictTypeStatusParam) {
        String url = serviceUrl + "/dmsap/dicttype/updateDictTypeStatus";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", AlertAgentServiceApplication.TOKEN);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<UpdateDictTypeStatusParam> request = new HttpEntity<>(updateDictTypeStatusParam, headers);
        CommonResult commonResult = restTemplate.postForObject(url, request, CommonResult.class);
        if (commonResult.getCode() != 200) {
            return commonResult.getMessage();
        }
        return "停用成功";
    }

}
